package com.zt.bus.line.controller;

import com.zt.bus.constant.BusConstant;
import com.zt.bus.line.model.Discount;
import com.zt.bus.line.model.DiscountDetail;
import com.zt.bus.line.service.DiscountService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.util.DateUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/discount")
public class DiscountController {

    @Autowired
    private DiscountService discountService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(discountService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        discountService.add(getDiscount(reqBean), getDiscountDetailList(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        discountService.update(getDiscount(reqBean), getDiscountDetailList(reqBean), getDeleteDiscountDetailList(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/deleteDetail")
    @ResponseBody
    public JSONObject deleteDetail(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        discountService.deleteDetail(reqBean.getInfo("discountDetailId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        discountService.delete(reqBean.getInfo("discountId"), reqBean.getUserId());
        return Response.responseSuccess();
    }

    /**
     * 从表单获取对象
     * 
     * @param reqBean
     * @return
     */
    private Discount getDiscount(ReqBean reqBean) {
        Discount discount = new Discount();
        discount.setId(reqBean.getInfo("discountId"));
        discount.setName(reqBean.getInfo("discountName"));
        discount.setType(reqBean.getInfo("discountType"));
        discount.setAmountGtThan(reqBean.getInfoDouble("amountGtThan"));
        discount.setStatus(reqBean.getInfo("discountStatus"));
        discount.setOperator(reqBean.getUserId());
        discount.setRemark(reqBean.getInfo("remark"));
        return discount;
    }

    private List<DiscountDetail> getDiscountDetailList(ReqBean reqBean) {
        String detailListStr = reqBean.getInfo("discountDetails");
        String discountType = reqBean.getInfo("discountType");
        JSONArray array = JSONArray.fromObject(detailListStr);
        List<DiscountDetail> list = new ArrayList<DiscountDetail>();
        if (array != null && array.size() > 0) {
            for (int i = 0; i < array.size(); i++) {
                JSONObject json = array.getJSONObject(i);
                DiscountDetail detail = new DiscountDetail();
                if (BusConstant.DiscountType.PERIOD_RATE.equals(discountType)) {
                    detail.setStartDate(DateUtil.parse(json.optString("startDate")));
                    detail.setEndDate(DateUtil.parse(json.optString("endDate")));
                } else {
                    detail.setDays(Integer.parseInt(json.optString("days")));
                }
                detail.setId(json.optString("discountDetailId"));
                detail.setStatus(json.getString("discountDetailStatus"));
                detail.setDiscountRate(Double.parseDouble(json.getString("discountRate")));
                detail.setDiscountId(reqBean.getInfo("discountId"));
                detail.setOperator(reqBean.getUserId());
                // detail.setMakeTime(new Date());
                // detail.setModifyTime(new Date());
                list.add(detail);
            }
        }

        return list;
    }

    private JSONArray getDeleteDiscountDetailList(ReqBean reqBean) {
        String deleteDetailListStr = reqBean.getInfo("deleteDetailList");
        return JSONArray.fromObject(deleteDetailListStr);
    }

}
